package com.videoaggre.zcl.ui.activity.qiman

import android.content.Context
import android.os.Bundle
import android.view.View
import android.view.ViewGroup
import androidx.lifecycle.lifecycleScope
import androidx.recyclerview.widget.LinearLayoutManager
import com.kingja.loadsir.core.LoadService
import com.scwang.smartrefresh.layout.api.RefreshLayout
import com.videoaggre.zcl.R
import com.videoaggre.zcl.bean.QimanComicPre
import com.videoaggre.zcl.common.QIMAN_SERIE_ID
import com.videoaggre.zcl.common.QIMAN_WORKS_ID
import com.videoaggre.zcl.common.showLoadingCallback
import com.videoaggre.zcl.common.startKtxActivity
import com.videoaggre.zcl.databinding.QimanItemComicPreBinding
import com.videoaggre.zcl.db.QimanComicBrowse
import com.videoaggre.zcl.db.mQimanDao
import com.videoaggre.zcl.helper.loadsir.LoadSirHelper
import com.videoaggre.zcl.parse.BaseParse
import com.videoaggre.zcl.parse.impl.QimanParse
import com.videoaggre.zcl.ui.activity.BaseActivity
import com.videoaggre.zcl.ui.activity.launchUI
import com.videoaggre.zcl.ui.adapter.BaseRecyclerAdapter
import com.videoaggre.zcl.ui.adapter.BaseViewHolder
import com.videoaggre.zcl.ui.fragment.parseHtml
import com.videoaggre.zcl.utils.GlideUtils
import com.videoaggre.zcl.utils.Utils
import kotlinx.android.synthetic.main.qiman_activity_browse.*
import kotlinx.coroutines.launch

class QimanBrowseActivity : BaseActivity(R.layout.qiman_activity_browse) {
    companion object {
        private const val LOAD = 1
        private const val REFRESH = 2
        private const val LOAD_MORE = 3
        fun openBrowse(context: Context, worksId: String, serieId: String) {
            context.startKtxActivity<QimanBrowseActivity>(extra = Bundle().apply {
                putString(QIMAN_WORKS_ID, worksId)
                putString(QIMAN_SERIE_ID, serieId)
            })
        }
    }

    private val mQimanComicPreAdapter: QimanComicPreAdapter = QimanComicPreAdapter()
    private var worksId: String = ""
    private var mSerieId: String = ""
    private val mLoadService: LoadService<*> by lazy { LoadSirHelper.register(rootView) { onReload() } }
    private var loadStatus: Int = LOAD
    private var mQimanComicBrowse: QimanComicBrowse? = null
    override fun initView() {
        Utils.invasionStatusBar(this)
        bindViewClick(ivClose, ivDir)
        refreshLayout.setEnableAutoLoadMore(true)
        refreshLayout.setEnableRefresh(false)
        refreshLayout.setOnRefreshListener(::onRefresh)
        refreshLayout.setOnLoadMoreListener(::onLoadMore)
        recyclerView.adapter = mQimanComicPreAdapter
        recyclerView.layoutManager = LinearLayoutManager(this)
    }

    override fun initData() {
        worksId = intent?.extras?.getString("worksId") ?: ""
        mSerieId = intent?.extras?.getString("serieId") ?: ""
        mLoadService.showLoadingCallback()
        onReload()
    }

    private fun onRefresh(refreshLayout: RefreshLayout) {
        loadStatus = REFRESH
    }

    private fun onLoadMore(refreshLayout: RefreshLayout) {
        loadStatus = LOAD_MORE
        mQimanComicBrowse?.apply {
            mSerieId = nextId
        }
        onReload()
    }

    private fun onReload() {
        launchUI({
            mQimanComicBrowse = mQimanDao.queryQimanComicBrowseById(mSerieId)
            if (mQimanComicBrowse == null)
                parseHtml(BaseParse(this@QimanBrowseActivity, this@QimanBrowseActivity, QimanParse.getQimanBrowse(worksId, mSerieId), loadService = mLoadService, refreshLayout = refreshLayout) {
                    lifecycleScope.launch {
                        mQimanComicBrowse = QimanParse.parseComicBrowse(mSerieId, it).also { mQimanDao.addQimanComicBrowse(it) }
                        handQimanComicBrowse()
                    }
                })
            else {
                mLoadService.showSuccess()
                handQimanComicBrowse()
            }
        })
    }

    private fun handQimanComicBrowse() {
        mQimanComicBrowse?.apply {
            when (loadStatus) {
                LOAD -> mQimanComicPreAdapter.datas = qimanComicPre ?: mutableListOf()
                //REFRESH -> mQimanComicPreAdapter.addAll(mQimanComicPres)
                LOAD_MORE -> mQimanComicPreAdapter.addAll(qimanComicPre)
            }
            mQimanComicPreAdapter.notifyDataSetChanged()
            if (nextId.isEmpty()) {
                refreshLayout.setNoMoreData(true)
            }
        }
    }

    inner class QimanComicPreAdapter : BaseRecyclerAdapter<BaseViewHolder<QimanItemComicPreBinding>, String>() {
        override fun onCreateViewHolder(parent: ViewGroup, p1: Int) = BaseViewHolder(QimanItemComicPreBinding.inflate(getLayoutInflate(parent.context), parent, false))
        override fun onBindViewHolder(holder: BaseViewHolder<QimanItemComicPreBinding>, bean: String, position: Int) {
            holder.mViewBinding.run {
                GlideUtils.intoView(ivImage.context, bean, ivImage)
                setViewClick(position, root)
            }
        }

        override fun subOnClickAdapter(v: View, position: Int) {
            //AgefansDetailsActivity.open(v.context, AgefansParse.createDetailLink(getItem(position).id))
        }
    }
}